package com.scau.microblogverse.common.result;

import com.scau.microblogverse.common.enumeration.ResCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {

    private Integer code; //编码：1成功，0和其它数字为失败
    private String msg; //错误信息
    private T data; //数据

    /**
     * 无参返回成功
     * @return
     */
    public static <T> Result<T> ok() {
        Result<T> result = new Result<T>();
        result.code = ResCode.OK.getCode();
        return result;
    }

    /**
     * 有参返回成功
     * @param object
     * @return
     */
    public static <T> Result<T> ok(T object) {
        Result<T> result = new Result<T>();
        result.data = object;
        result.code = ResCode.OK.getCode();
        return result;
    }

    /**
     * 带有提示信息返回成功
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> ok(String message){
        Result<T> result = new Result<T>();
        result.msg = message;
        result.code = ResCode.OK.getCode();
        return result;
    }

    /**
     * 带有数据、提示信息返回成功
     * @param object
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> ok(T object, String message){
        Result<T> result = new Result<T>();
        result.data = object;
        result.msg = message;
        result.code = ResCode.OK.getCode();
        return result;
    }

    /**
     * 无参返回失败
     * @return
     * @param <T>
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<T>();
        result.code = ResCode.ERR.getCode();
        result.data = null;
        result.msg = "请求错误!";
        return result;
    }

    /**
     * 带有错误信息返回失败
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.msg = msg;
        result.code = ResCode.ERR.getCode();
        result.data = null;
        return result;
    }

}
